
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="渠道名称:" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入渠道名称" />
          </el-form-item>
          <el-form-item label="CODE:" prop="code">
            <el-input v-model="ruleForm.code" placeholder="请输入渠道代码" />
          </el-form-item>
          <el-form-item label="线路:" prop="line_id">
            <el-select
              v-model="ruleForm.line_id"
              style="width:100%"
              placeholder="请选择线路"
            >
              <el-option
                v-for="(item,index) in linesList"
                :key="index"
                :label="item.line_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="业务类型:" prop="type">
            <el-select
              v-model="ruleForm.type"
              style="width:100%"
              placeholder="请选择业务类型"
            >
              <el-option
                label="转运"
                :value="1"
              />
              <el-option
                label="仓储"
                :value="2"
              />
              <el-option
                label="自送"
                :value="3"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="承运商:" prop="carrier_id">
            <el-select
              v-model="ruleForm.carrier_id"
              style="width:100%"
              placeholder="请选择承运商"
            >
              <el-option
                v-for="(item,index) in expressList"
                :key="index"
                :label="item.company_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="价格模版:" prop="price_template_id">
            <el-select
              v-model="ruleForm.price_template_id"
              style="width:100%"
              placeholder="请选择价格模版"
              filterable
            >
              <el-option
                v-for="(item,index) in priceList"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="结算币种:" prop="currency">
            <el-select
              v-model="ruleForm.currency"
              style="width:100%"
              placeholder="请选择结算币种"
            >
              <el-option
                label="人民币"
                value="RMB"
              />
              <el-option
                label="韩元"
                value="KRW"
              />
              <el-option
                label="美元"
                value="USD"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="大小件:" prop="size">
            <el-select
              v-model="ruleForm.size"
              style="width:100%"
              placeholder="请选择大小件"
            >
              <el-option
                label="小件"
                :value="1"
              />
              <el-option
                label="大件"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="服务时效:" prop="service_time_start">
            <el-select
              v-model="time"
              style="width:100%"
              placeholder="请选择服务时效"
              clearable
              @change="chengeTime"
            >
              <el-option
                label="5-7天"
                value="1"
              />
              <el-option
                label="15天"
                value="2"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="ruleForm.status"
              style="width:100%"
              placeholder="请选择状态"
            >

              <el-option
                label="启用"
                :value="1"
              />
              <el-option
                label="禁用"
                :value="0"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否私有:" prop="is_private">
            <el-select
              v-model="ruleForm.is_private"
              style="width:100%"
              placeholder="请选择状态"
            >

              <el-option
                label="私有"
                :value="1"
              />
              <el-option
                label="公开"
                :value="0"
              />
            </el-select>
          </el-form-item>

        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createChannel, updateChannel } from '@/api/vocational/irrigation'
import { listFreight } from '@/api/vocational/price'

import { listCourierNo } from '@/api/base/express'
import { listLineNo } from '@/api/base/lines'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      time: '',
      showDialog: false,
      expressList: [],
      linesList: [],
      priceList: [],
      ruleForm: {
        'line_id': '',
        'name': '',
        'code': '',
        'carrier_id': '',
        'type': '',
        'currency': '',
        'price_template_id': '',
        'service_time_start': '',
        'service_time_end': '',
        'status': '',
        'size': '',
        'is_private': ''
      },
      rules: {
        name: [
          { required: true, message: '请输入渠道名称', trigger: 'blur' }
        ],
        // code: [
        //   { required: true, message: '请输入渠道代码' }
        // ],
        line_id: [
          { required: true, message: '请选择线路' }
        ],
        type: [
          { required: true, message: '请选择业务类型' }
        ],
        carrier_id: [
          { required: true, message: '请选择承运商' }
        ],
        price_template_id: [
          { required: true, message: '请选择价格模版' }
        ],
        currency: [
          { required: true, message: '请选择结算币种' }
        ],
        size: [
          { required: true, message: '请选择大小件' }
        ],
        // service_time_start: [
        //   { required: true, message: '请选择服务时效' }
        // ],
        status: [
          { required: true, message: '请选择状态' }
        ],
        is_private: [
          { required: true, message: '请选择是否私有' }

        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        if (this.status === 2) {
          Object.keys(this.ruleForm).forEach(i => {
            this.ruleForm[i] = this.detial[i]
          })
          this.ruleForm.id = this.detial.id
          if (this.detial.service_time_start === 5) {
            this.time = '1'
          } if (this.detial.service_time_start === 15) {
            this.time = '2'
          } else {
            this.time = ''
          }
        }
      }
    }
  },
  mounted() {
    this.getExpressCode()
    this.getLines()
    this.getPriceList()
  },
  methods: {
    chengeTime() {
      if (this.time === '1') {
        this.ruleForm.service_time_start = 5
        this.ruleForm.service_time_end = 7
      } else {
        this.ruleForm.service_time_start = 15
        this.ruleForm.service_time_end = 15
      }
    },
    getPriceList() {
      listFreight({
        limit: 10000,
        status: 1
      }).then(res => {
        this.priceList = res.data.data
        // this.priceList.unshift({ name: '暂无', id: 0 })
      })
    },
    getExpressCode() {
      listCourierNo({
      }).then(res => {
        this.expressList = res.data
      })
    },
    getLines() {
      listLineNo({
      }).then(res => {
        this.linesList = res.data
      })
    },
    close() {
      this.$emit('update', false)
      this.time = ''
      this.ruleForm = {
        'line_id': '',
        'name': '',
        'code': '',
        'carrier_id': '',
        'type': '',
        'currency': '',
        'price_template_id': '',
        'service_time_start': '',
        'service_time_end': '',
        'status': '',
        'size': '',
        'is_private': ''
      }
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createChannel(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateChannel(this.ruleForm).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
